package com.databias.j15.perfmon;

import java.awt.Font;

import com.databias.j15.*;
import com.databias.util.*;

import org.nsclient4j.NSClient4JException;
import org.nsclient4j.NSClient4j;

/**
 * Keep track of the components.  
 * 
 * To update the components, send the client reference here.
 * @author Frederick
 *
 */

public class PerfComponents {

	
	GGauge cpuGauge, tcpGauge, fileGauge;
	GBar2 pageBar, tcpBar;
	GText ipText;
	
	RollingAverage fileValue;
	RollingAverage tcpValue;
	RollingAverage pageValue;
	
	NSClient4j client;
	
	public PerfComponents (String host)
	{

		// This one is for CPU
		cpuGauge = new GGauge(60, 0);
		cpuGauge.setSolid(false);
		cpuGauge.showText(true);
		
		// A gauge for network
		tcpGauge = new GGauge(30, 0);
		tcpGauge.setFilled(false);
		tcpGauge.setTics(false);
		tcpGauge.showText(false);


		// A gauge pointer for file switching
		fileGauge = new GGauge(60, 0);
		fileGauge.setSolid(false);
		fileGauge.showText(true);
		//fileGauge.setFilled(false);
		
		// A bar for paging
		pageBar = new GBar2(4, 20, GBar.VERTICAL, GBar.NOOUTLINE);
		pageBar.setMaxValue(500); 
		
		// And a text value for it
		ipText= new GText("Net", new Font ("Dialog", Font.PLAIN, 9));
		
		
		// Create the connection to the service
		try {
			client = new NSClient4j(host);
			// client.getPerfMonCounter(arg0)

		} catch (NSClient4JException e) {
			System.err.println("Exception Geting Stat:" + e);
		}

		// Store the averages
		fileValue = new RollingAverage(10, 3);
		tcpValue = new RollingAverage(10);
		pageValue = new RollingAverage(10, 3);

	}
	
	/**
	 * Add our components to the GScreen
	 * 
	 * @param screen
	 */
	void addToScreen (GScreen screen)
	{
		screen.add(95, 12, cpuGauge);
		screen.add(115,8, new GText("CPU", new Font ("Dialog", Font.PLAIN, 10)));
		
		screen.add(65, 12, tcpGauge);
		GText tcpText=new GText("IP");
		tcpText.setFont(new Font ("Dialog", Font.PLAIN, 10));
		screen.add(75,8, tcpText);

		screen.add(72,35, ipText);
		
		screen.add(5, 12, fileGauge);
		screen.add(26,8, new GText("File", new Font ("Dialog", Font.PLAIN, 10)));
		
		screen.add(3,28, pageBar);
		screen.add(3,8, new GText("P", new Font ("Dialog", Font.PLAIN, 8)));
		

		
	}
	
	/**
	 * Update our components, since this is the only one who knows about them
	 * make calls to the client...
	 */
	void updateComponents()
	{
		
		try {
		
			cpuGauge.setValue((int)Float.parseFloat(
					client.getPerfMonCounter("\\Processor(_Total)\\% Processor Time"))); // "CPU" is 5-min average?
			
			
			fileGauge.setValue(fileValue.getAverage((100*(int)Float.parseFloat(
					client.getPerfMonCounter("\\PhysicalDisk(_Total)\\Avg. Disk Queue Length")))));
			
			System.out.println
			(client.getPerfMonCounter("\\PhysicalDisk(_Total)\\Avg. Disk Queue Length"));
			
			System.out.println(fileValue.getAverage());
			
			tcpGauge.setValue(tcpValue.getAverage((int)Float.parseFloat(
					client.getPerfMonCounter("\\IP\\Datagrams/sec"))/10));
			//ipText.setText(Integer.toString(tcpValue.getAverage()));
			ipText.setText(Integer.toString(
					(int)Float.parseFloat(client.getPerfMonCounter("\\IP\\Datagrams/sec"))));

			
			pageBar.setValue(pageValue.getAverage((int)Float.parseFloat(
					client.getPerfMonCounter("\\Memory\\Pages/sec"))));
			

		}
		catch (NSClient4JException e) {
			// Ideally, put in some code to put the exception to the screen. 
			// Maybe change this method to throw this exception so the caller
			// can update as needed.
			System.err.println("Exception Getting Stat:" + e);
			

		}
		catch (Exception e)
		{
			System.err.println("Exception starting PM:"+e);
		}
	}
}
